#!/usr/bin/python

import sys
from time import sleep

''' Using the formula of the wiki of tinyOS for telosB '''

def convert_time(t):
	'''Convert timestamp.
	Does nothing for now.'''
	return t

def convert_voltage(v):
	'''Convert voltage.
	This is already done on the mote'''
	return v
	
def convert_temperature(v):
	v = -38.4 + 0.0098 * float(v)
	return v
	
def convert_humidity(v, temperature):
	rh_initial = float(v)
	rh_linear = -4 + 0.0405 * rh_initial - 0.0000028 * pow(rh_initial,2)
	rh_real = (temperature - 25) * (0.01 + 0.00008 * rh_initial) + rh_linear
	v = rh_real
	return v
	
def convert_light(v):
	return v

def main():
	'''Convert values.'''
	if len(sys.argv) < 2:
		infile = sys.stdin
#		sys.stderr.write("Usage: " + sys.argv[0] + " <infile>\n")
	else:
		infile = open(sys.argv[1], "r")

	while True:
		line = infile.readline()

		if len(line) > 0:
			try:
				if line[0] == '#':
					sys.stdout.write(line)
					sys.stdout.flush()
				else:
					data = line.split()
					
					time = convert_time(data[0])
					voltage = convert_voltage(data[1])
					temperature = convert_temperature(data[2])
					humidity = convert_humidity(data[3], temperature)
					light1 = convert_light(data[4])
					light2 = convert_light(data[5])
					
					sys.stdout.write(" ".join([str(i) for i in [time, voltage, temperature, humidity, light1, light2]]) + '\n')
					sys.stdout.flush()
			except:
				#sys.stderr.write('convert failed on input '+line)
				#sys.stderr.flush()
				pass
		else:
			sleep(1)


if __name__ == '__main__':
	main()
